package it312l.group9.itorf;
import java.util.LinkedList;
import java.util.List;
 
import it312l.group9.itorf.Question;
 
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
public class MySQLiteHelper extends SQLiteOpenHelper {
 
    // Database Version
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "itorf.db";
 
    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);  
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
        // SQL statement to create book table
        String CREATE_QUESTIONS_TABLE = "CREATE TABLE questions ( " +
                "q_id INTEGER PRIMARY KEY, " + 
                "q_desc CHAR(200), "+
                "answer CHAR(5) References answer, ans_desc CHAR(200) )";
 
        // create books table
        db.execSQL(CREATE_QUESTIONS_TABLE);
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older books table if existed
        db.execSQL("DROP TABLE IF EXISTS questions");
 
        // create fresh books table
        this.onCreate(db);
    }
    //---------------------------------------------------------------------
 
    /**
     * CRUD operations (create "add", read "get", update, delete) book + get all books + delete all books
     */
 
    // Books table name
    private static final String QUESTIONS = "questions";
 
    // Books Table Columns names
    private static final String qID = "q_id";
    private static final String QDesc = "q_desc";
    private static final String answer = "answer";
    private static final String ansDesc= "ans_desc";
 
    private static final String[] COLUMNS = {qID,QDesc,answer,ansDesc};
 
 
    public Question getQuestion(){
 
        // 1. get reference to readable DB
        SQLiteDatabase db = this.getReadableDatabase();
 
        // 2. build query
        Cursor cursor = 
                db.query(QUESTIONS, // a. table
                null, // b. column names
                null, // c. selections 
                null, // d. selections args
                null, // e. group by
                null, // f. having
                "Random()", // g. order by
                "1"); // h. limit
 
        // 3. if we got results get the first one
        if (cursor != null)
            cursor.moveToFirst();
 
        // 4. build book object
        Question question = new Question(cursor.getString(1), Boolean.parseBoolean(cursor.getString(2)), cursor.getString(3));
 
 
        // 5. return book
        return question;
    }
 

}